class OrderReport < Prawn::Document

  include AbstractController::Translation
  include ActionView::Helpers::NumberHelper

  def to_pdf_http(cart, user)
    generate_content(cart, user)
    render
  end

  def to_pdf_file(cart, user)

    dir = Rails.root + "tmp/pdf/"
    filename = t("order.model_name") + user.id.to_s + "_" + cart.id.to_s + ".pdf"
    path = dir + filename
    generate_content(cart, user)
    render_file(path)
    return path
  end

  def generate_content(cart, user)
    repeat :all do

      header
      #footer

    end

    bounding_box([bounds.left, bounds.top - 50], :width => bounds.width, :height => bounds.height - 100) do
      #Address Blco
      user_address(user)

      #Date
      date

      move_down(150)
      salutation(user, cart)
      move_down(30)
      order_content(cart)

    end
  end


  private

  def header
    bounding_box [bounds.left, bounds.top], :width => bounds.width do

      y_position = cursor
      font "Helvetica"
      #text "Here's My Fancy Header", :align => :center, :size => 25
      logopath = "#{Rails.root}/app/assets/images/RailsShopLogoNurText.jpg"
      image logopath, :width => 150, :at => [10, 20]
      shop_address
      move_down 25
      stroke_horizontal_rule
    end
  end

  def footer
    bounding_box [bounds.left, bounds.bottom + 20], :width => bounds.width do
      stroke_horizontal_rule
    end

    bounding_box [bounds.left, bounds.bottom + 10], :width => bounds.width do
      font "Helvetica"
      string = "#{t "helpers.basic.page"} <page> #{t "helpers.basic.from"} <total>"
      number_pages string
    end
  end

  def user_address(user)
    text_box "#{user.title}\n" +
                 "#{user.firstname} #{user.lastname}\n" +
                 "#{user.street}\n" +
                 "#{user.zip} #{user.city}",
             :at => [0, bounds.top - 30]
  end

  def date
    text_box "Bern, #{l(Time.now.to_date)}",
             :at => [bounds.width - 150, bounds.top - 30]
  end

  def salutation(user, cart)

    order_date = l(cart.updated_at.to_s[0..9].to_date)

    text "<font size='18'>#{t("order.letter.title")} #{order_date}</font>", :inline_format => true
    move_down(20)

    case user.title
      when "Herr"
        title_text = "#{t("order.letter.salutation.mr")}"
      when "Frau"
        title_text =  "#{t("order.letter.salutation.ms")}"
      when "Firma"
        title_text =  "#{t("order.letter.salutation.company")}"
    end
    text "#{title_text} #{user.lastname}"
    move_down(10)
    text "#{t("order.letter.text", :datum => order_date)}"
  end

  def order_content(cart)
    #Header of the table
    items = [[
                 "<b>#{t('product.model_name')}</b>",
                 "<b>#{t("product.amount")}</b>",
                 "<b>#{t("product.price")}</b>",
                 "<b>#{t("product.total")}</b>"
             ]]

    items += cart.cart_items.map do |item|
      [
          item.product.name,
          item.amount,
          number_to_currency(item.price),
          number_to_currency(item.amount * item.price)
      ]
    end

    table(items, :header => true, :column_widths => [250, 60, 100, 100],
          :cell_style => {:inline_format => true})

    move_down(20)
    text "<font size='18'>#{t("product.total")}: #{number_to_currency(cart.total)}</font>", :inline_format => true

  end

  def shop_address
    text_box "RailsShop\nBahnhofstrasse 1\n 3000 Bern",
             :at => [bounds.width - 100, bounds.top + 18], :width => 100
  end

end